import serial
import time

ser = serial.Serial(
    port='/dev/ttyAMA3',
    baudrate=115200,
    timeout=1
)

if not ser.is_open:
    ser.open()
print("串口已打开")

try:
    # start_time = time.time()  # 记录开始时间
    # while time.time() - start_time < 10:  # 循环10秒后退出
    while True:
        if ser.in_waiting > 0:
            recv_data = ser.read(ser.in_waiting).decode('utf-8')
            print(f"收到数据：{recv_data.strip()}")
        time.sleep(0.1)
finally:
    ser.close()
    print("串口已关闭")